<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\demo\controller;

use cmf\controller\RestBaseController;
use think\Db;

class ArticlesController extends RestBaseController
{
    public function index()
    {
        echo '非法请求！！！';
    }
    //获取所有医生信息
    public function getDoctors()
    {
    	$data = Db::name("doctor")->select();
    	if($data){
            return success(1,"获取医师列表成功",$data);
        }
        return success(0,"获取医师列表失败");
    }
    //获取单个医生信息
    public function getOneDoctor()
    {
    	$id = input('id');
    	$data = Db::name("doctor")->find($id);
    	if($data){
            return success(1,"获取医师详情成功",$data);
        }
        return success(0,"获取医师详情失败");   	
    }
    //获取问诊信息
    public function getInquiry()
    {
    	$id = input('id');
		$where = [
			'q_did' =>$id
		];
    	$questiondata = Db::name('question') ->where($where)
    	->paginate(50)->each(function($item,$key){
			$item['answerdata'] = Db::name('answer') ->where('a_qid',$item['q_id'])->select();
            return $item;
        })->toArray();
		if($questiondata){
           	return success(1,"获取学堂列表成功",$questiondata);
        }
        return success(0,"获取学堂列表失败",$questiondata);
    }
    //获取患者信息
    public function gethuanzhe(){
    	$uid = input('uid');
    	$data = Db::name('sufferer') ->where('s_uid',$uid)->where('s_isshow',1) ->find();
    	if($data){
           	return success(1,"获取默认患者成功",$data);
        }else{
        	$data2 = Db::name('sufferer') ->where('s_uid',$uid) ->find();
        	if($data2){
        		return success(1,"获取默认患者成功",$data2);
        	}
        }
        return success(0,"获取默认患者失败",$data);
    }
    //获取患者信息
    public function gethuanzhes(){
    	$uid = input('uid');
    	$data = Db::name('sufferer') ->where('s_uid',$uid) ->select();
    	if($data){
           	return success(1,"获取患者列表成功",$data);
        }
        return success(0,"获取患者列表失败",$data);
    }
    //新增患者信息
    public function addhuanzhe(){
    	$data = input();
    	//将数据添加到数据库
    	$arr = Db::name('sufferer')->where('s_uid',$data['uid']) ->where('s_name',$data['name'])->find();
    	if($arr){
    		return success(1,"已添加该患者",false);
    	}else{
    		$adddata = [
    			's_name' =>$data['name'],
    			's_sex' =>$data['sex'],
    			's_age' =>$data['age'],
    			's_hight' =>$data['height'],
    			's_weight' =>$data['weight'],
    			's_uid' =>$data['uid'],
    			's_filiation' =>$data['guanxi'],
    			's_phone' =>$data['phone'],
    		];
    		if($data['isshow']){
    			$adddata['s_isshow'] =1;
    			$data2['s_isshow'] =0;
    			$num = Db::name('sufferer')->count();
    			if($num==0){
    				$res1 = true;
    			}else{
    				$res1 = Db::name('sufferer')->where('s_uid',$adddata['s_uid']) ->update($data2);    				
    			}	
    			if($res1){
    				$res = Db::name('sufferer') ->insert($adddata);
    			}else{
    				return success(1,"添加患者失败",false);
    			}

    		}else{
				$adddata['s_isshow'] =0;
				$res = Db::name('sufferer') ->insert($adddata);

    		}
    		return success(1,"添加患者成功",true);
    	}
    }
    //设置默认
    public function setmoren(){
    	$id = input('id');
    	//修改默认
    	$data['s_isshow'] =1;
    	$arr = Db::name('sufferer') ->find($id); 
    	$uid = $arr['s_uid'];
    	$data2['s_isshow'] = 0;
    	$res1 = Db::name('sufferer')->where('s_uid',$uid) ->update($data2);
    	$res = Db::name('sufferer')->where('s_id',$id) ->update($data); 
    	if($res){
    		return success(1,"设置默认成功",true);
    	}
    	return success(1,"设置默认失败",false);
    }
    //进行预约
    public function addorder()
    {
    	$data = input();
    	$datas = json_decode($data['datas'],true);
		$a_content = json_decode($data['a_content'],true);
    	//先判断当前是否能够预约
    	$alldata = Db::name('order') ->select();
    	$num = 0;

    	foreach ($alldata as $key => $value) {
    		if(date("Y-m-d",strtotime($value['o_time'])) ==$datas['date']){
    			$num++;
    		}
    	}
    	//获取总数
    	$allnum = Db::name('doctor') ->where('d_id',$datas['did'])->value('d_topnum');
    	if($num>=$allnum){
    		return success(1,"今日已到达上限",false);
    	}else{
    		//获取患者id
    		$sid = Db::name('sufferer') ->where('s_uid',$data['uid'])->where('s_isshow',1)->value('s_id');
    		//处理文件上传然后添加数据库order
    		$time = $datas['date']."\n0".$datas['time'];
    		$orderdata = [
    			'o_sid' =>$datas['sid'],
    			'o_time' =>$time,
    			'o_desc' =>$datas['desc'],
    			'o_pic1' =>$data['img2'],
    			'o_pic2' =>$data['img3'],
    			'o_ischuli' =>0,
    			'o_isguoqi' =>0,
    			'o_did' =>$datas['did'],
    		];
    		$oid = Db::name('order') ->insertGetId($orderdata);
    		//添加图片
            if($data['img1']){
    		$img1 = explode(',',$data['img1']);
            
        		foreach ($img1 as $key => $value) {
        			$picdata = [
    	    			'p_src' =>$value,
    	    			'p_oid' =>$oid
    	    		];
    	    		$picres = Db::name('picture') ->insert($picdata);
        		}
            }

    		//添加问卷信息
    		if(is_array($a_content)){
    			$wenzhenarr = [];
	    		foreach ($a_content as $key => $value) {
	    			$aids = implode($value,',');
	    			$wenzhenarr['w_qid'] = $key;
	    			$wenzhenarr['w_aid'] = $aids;
	    			$wenzhenarr['w_oid'] = $oid;
	    			$wenres = Db::name('wenzhen') ->insert($wenzhenarr);
	    		}
	    		
	    	}
	    	return success(1,"预约成功",true);

    		
    	}
    }




    //获取预约列表
    public function getOrders()
    {
    	$uid = input('id');
    	$sids = Db::name('sufferer') ->where('s_uid',$uid) ->select();
    	$data = [];
    	foreach ($sids as $key => $value) {
    		$data = Db::name('order') ->alias('o')
    		->join("sufferer s","o.o_sid=s.s_id","LEFT")
    		->join("doctor d",'o.o_did =d.d_id',"LEFT")
    		 ->where('o.o_sid',$value['s_id']) ->select();
    	}
    	return success(1,"获取预约信息成功",$data);
    }
    //取消预约
    public function quxiao(){
    	$id = input('id');
    	$data = [
    		'o_ischuli'=>2
    	];
    	$res = Db::name('order') ->where('o_id',$id) ->update($data);
    	if($res){
    		return success(1,"取消预约成功",true);
    	}
    	return success(1,"取消预约失败",false);
    }





    //文件上传
    public function imgupload(){
    	$file = request()->file("file");
        if ($file) {
            $info = $file->move('uploads/weixin/');
            if ($info) {
                $file_name = $info->getSaveName();
                $file_name_arr = explode("\\",$file_name);
                $file_names = implode("/",$file_name_arr);
                $file_names = $this->request->domain()."/uploads/weixin/".$file_names;
                return $file_names;
            }
        }
    }












    //获取openid进行注册或者登陆
    public function getOpenid()
    {
    	$code = input('code');
    	$url = "https://api.weixin.qq.com/sns/jscode2session?appid=wx1a5ae9eeea406181&secret=1ce20a84b08118c45dd7102a858d0b78&js_code=".$code."&grant_type=authorization_code";
    	$data = httprequest($url,true);
    	$data = json_decode($data,true);
    	$openid = $data['openid'];
    	//查询用户数据
    	$userdata = Db::name('wxuser') ->where('openid',$openid) ->find();
    	if($userdata){
    		$uid = $userdata['id'];
    	}else{
    		$arr = [
    			'openid' =>$openid
    		];
    		$uid = Db::name('wxuser') ->insertGetId($arr);
    	}
    	return $uid;

    }
}
